package edu.xjtlu.mobilewebservices.voice;

import java.util.ArrayList;
import edu.xjtlu.mobilewebservices.R;
import edu.xjtlu.mobilewebservices.webservice.Barcode2WS;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.ActivityNotFoundException;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

public class VoiceRecord extends Activity {
	private static final int VOICE_RECOGNITION_REQUEST_CODE = 4321;
	String product_name ="Product Name";
	String shop_name ="Shop Name";
	String Barcode = "Barcode";
	EditText edittext;
	RadioButton rButton1,rButton2,rButton3;
	int rButton_checkedId;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.voice);
		
		ImageView voice_button = (ImageView) findViewById(R.id.voice_search_button);
		ImageView nextstep_button = (ImageView) findViewById(R.id.voice_nextstep);
		edittext =(EditText) findViewById(R.id.et1); 
		
		//RadioButton Group
		RadioGroup rGroup = (RadioGroup)findViewById(R.id.radiogroup);
		rButton1 = (RadioButton) findViewById(R.id.radioButton1);
		rButton2 = (RadioButton) findViewById(R.id.radioButton2);
		rButton3 = (RadioButton) findViewById(R.id.radioButton3);
		
		//RadioButton listener
		rGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				rButton_checkedId = checkedId;
			}
		});
		
		//initial voice recognizer function with click button
		voice_button.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				try
				{
			      //start Voice Recognizer through Intent 
				 Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);	
				 
				 //setting languages of Voice Recognizer 
				 intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
				 
				 //notification of starting voice recognizer
				 intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Start talk!");
				 
				 //start voice recognizer 
				 startActivityForResult(intent, VOICE_RECOGNITION_REQUEST_CODE);
				}
				catch(ActivityNotFoundException  e) {
				//cannot find device
				Toast.makeText(VoiceRecord.this, "ActivityNotFoundException ", Toast.LENGTH_SHORT).show();
				}
			}
		});
		
		nextstep_button.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent();
				Bundle bundle= new Bundle();
			
			if("".equals(edittext.getText().toString().trim())) {
				Toast.makeText(VoiceRecord.this, "Pls input your search item!", Toast.LENGTH_SHORT).show();
		     }else
				if (rButton_checkedId == rButton1.getId()){
					bundle.putString("key_search_item", edittext.getText().toString());
					bundle.putString("key_search_type",product_name);
					intent.putExtras(bundle);
					intent.setClass(VoiceRecord.this, Barcode2WS.class);
					startActivity(intent);
				}else
					if (rButton_checkedId == rButton2.getId()){
						bundle.putString("key_search_item", edittext.getText().toString());
						bundle.putString("key_search_type",shop_name);
						intent.putExtras(bundle);
						intent.setClass(VoiceRecord.this, Barcode2WS.class);
						startActivity(intent);
					}else
						if(rButton_checkedId == rButton3.getId()){
							bundle.putString("key_search_item", edittext.getText().toString());
							bundle.putString("key_search_type",Barcode);
							intent.putExtras(bundle);
						    intent.setClass(VoiceRecord.this, Barcode2WS.class);
						    startActivity(intent);
						  }else 
						  {
							  Toast.makeText(VoiceRecord.this, "Pls chooose your search type!", Toast.LENGTH_SHORT).show();
						  }
			}
		});
	}
	
	// return onActivityResult when finished
	protected void onActivityResult(int requestCode,int resultCode,Intent data){
		//judge if the voice recognize really execute 
		if(requestCode == VOICE_RECOGNITION_REQUEST_CODE && resultCode==RESULT_OK){
			//get char from voice recognizer
			ArrayList<String> result = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
			Toast.makeText(this, result.get(0), Toast.LENGTH_LONG).show();
		
				final CharSequence[] items =new CharSequence[result.size()];
				for (int i =0; i< result.size();i++){
					items[i]= result.get(i);
				}
				AlertDialog.Builder builder = new AlertDialog.Builder(this);
				builder.setTitle("Voice Result");
				builder.setItems(items, new DialogInterface.OnClickListener(){
					@Override
					public void onClick(DialogInterface dialog, int item) {
							edittext.setText(items[item]);
					}	
				});
				builder.create().show();
			super.onActivityResult(requestCode, resultCode, data);  
		}
	}
}
